#include "hx711.h"

long HX711_Buffer = 0;
long Weight_Maopi = 0,Weight_Shiwu = 0;

int _sck = 0;
int _dt  = 0;

#define GapValue 430

//****************************************************
//初始化HX711
//****************************************************
void Init_Hx711(int sck, int dt)
{
  _sck = sck;
  _dt = dt;
  pinMode(_sck, OUTPUT);  
  pinMode(_dt, INPUT);
}


//****************************************************
//获取毛皮重量
//****************************************************
void Get_Maopi()
{
  Weight_Maopi = HX711_Read();    
} 

//****************************************************
//称重
//****************************************************
long Get_Weight()
{
  HX711_Buffer = HX711_Read();
  Weight_Shiwu = HX711_Buffer;
  Weight_Shiwu = Weight_Shiwu - Weight_Maopi;       //获取实物的AD采样数值。
  Weight_Shiwu = (long)((float)Weight_Shiwu/GapValue);  
  return Weight_Shiwu;
}

//****************************************************
//读取HX711
//****************************************************
unsigned long HX711_Read(void)  //增益128
{
  unsigned long count; 
  unsigned char i;
  bool Flag = 0;

  digitalWrite(_dt, HIGH);
  delayMicroseconds(1);

  digitalWrite(_sck, LOW);
  delayMicroseconds(1);

    count=0; 
    while(digitalRead(_dt)); 
    for(i=0;i<24;i++)
  { 
      digitalWrite(_sck, HIGH); 
    delayMicroseconds(1);
      count=count<<1; 
    digitalWrite(_sck, LOW); 
    delayMicroseconds(1);
      if(digitalRead(_dt))
      count++; 
  } 
  digitalWrite(_sck, HIGH); 
  count ^= 0x800000;
  delayMicroseconds(1);
  digitalWrite(_sck, LOW); 
  delayMicroseconds(1);
  
  return(count);
}

